package com.base.user.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import cn.mysteel.util.ObjectUtils;

import com.base.kongjian.service.KongjianService;
import com.base.user.dao.MemberDao;
import com.base.user.entity.Member;
import com.base.user.service.MemberService;
import com.base.user.utils.MD5;
import com.mysteel.page.Page;

@Service("memberService")
public class MemberServiceImpl implements MemberService
{
	@Autowired
	private MemberDao memberDao;
	@Autowired
	private KongjianService kongjianService;
	
	@Override
	public void register(Member member)
	{
		Assert.notNull (member);
		
		String email = member.getEmail ();
		
		Map<String, Object> params = new HashMap<String,Object>();
		
		params.put ("email" ,email);
		Member m = memberDao.getByMap  (params);
		if(ObjectUtils.notNull (m))
		{
			return ;
		}
		params.remove ("email");
		params.put ("nickName" ,member.getNickName ());
		m = memberDao.getByMap  (params);
		if(ObjectUtils.notNull (m))
		{
			return ;
		}
		
		int count = memberDao.count (null);
		
		if(count==0)
		{
			member.setId (10000);
		}
		
		member.setRegisterTime (System.currentTimeMillis ());
		String password = member.getPassword ();
		member.setPassword (MD5.convertMD5(password));
		
		memberDao.save (member);
	}
	
	@Override
	public Map<String, Object> login(Map<String, Object> params)
	{
		Assert.notEmpty (params);
		
		Map<String, Object> paraMap = new HashMap<String,Object>();
		Map<String, Object> returnMap = new HashMap<String,Object>();
	
		String password = (String) params.get ("password");
		long id = (long) params.get ("id");
		
		String lastLoginIp = (String)params.get ("lastLoginIp");//IP
		String email = (String)params.get ("email");
		
		paraMap.put ("email" ,email);
		paraMap.put ("status" ,1);
		
		Member member = null;
		member = memberDao.getByMap (paraMap);
		String pass = "";
		
		if(ObjectUtils.isNull (member))
		{
			paraMap.remove ("email");
			paraMap.put ("id" ,id);
			
			member = memberDao.getByMap (paraMap);
		}
		
		if (ObjectUtils.notNull (member))
		{
			int status = member.getStatus ();
			if (status == 1)
			{
				pass = MD5.convertMD5 (member.getPassword ());
				
				if (pass.equals (password))
				{
					member.setLastLoginIp (lastLoginIp);
					member.setLastLoginTime (System.currentTimeMillis ());
					member.setLoginStatus (1);
					memberDao.update (member);
					kongjianService.login (member.getId ());//空间登录
					returnMap.put ("member" ,member);
				}else
				{
					
					returnMap.put ("loginMessage" ,"密码错误！");
				}
			}else
			{
					returnMap.put ("loginMessage" ,"账号异常，请联系客服");
			}
		}else
		{
			returnMap.put ("loginMessage" ,"用户名/ID/邮箱不存在");
		}
		
		return returnMap;
	}
	
	@Override
	public Page<Member> queryMember(Map<String, Object> params,int currentPageNum, int maxPageRowCount)
	{
		
		Page<Member> page = null;

		if (ObjectUtils.isNull(params))
		{
			params = new HashMap<String, Object>(2);
		}
		
		int count = memberDao.count(params);

		if (count > 0)
		{
			page = new Page<Member>();
			page.setTotalRowCount(count);
			page.setCurrentPageNum(currentPageNum);
			page.setMaxPageRowCount(maxPageRowCount);
			page.setURL(params);

			params.put("startPageRowCount", page.getStartPageRowCount());
			params.put("maxPageRowCount", page.getMaxPageRowCount());

			List<Long> mIds = memberDao.query(params);

			if (ObjectUtils.notEmpty(mIds))
			{
				List<Member> members = new ArrayList<Member>(mIds.size());

				for (long memberId : mIds)
				{
					members.add(getMemberById (memberId));
				}

				page.setDatas(members);
			}
		}

		return page;
	}
	
	@Override
	public void deleteMemberById(long id)
	{
		Member member = memberDao.getById (id);
		
		if(ObjectUtils.notNull (member))
		{
			memberDao.deleteById (id);
		}
		
	}
	
	@Override
	public void updateMember(Member member)
	{
		long id = member.getId ();
		
		Member me = memberDao.getById (id);
		
		if(ObjectUtils.notNull (me))
		{
			me.setAge (member.getAge ());
			me.setBirthDay (member.getBirthDay ());
			me.setEmail (member.getEmail ());
			me.setGender (member.getGender ());
			me.setHeadUrl (member.getHeadUrl ());
			me.setLastLoginIp (member.getLastLoginIp ());
			me.setLastLoginTime (member.getLastLoginTime ());
			me.setLoginStatus (member.getLoginStatus ());
			me.setNickName (member.getNickName ());
			me.setPassword (MD5.convertMD5 (member.getPassword ()));
			me.setSchool (member.getSchool ());
			me.setSign (member.getSign ());
			me.setStatus (member.getStatus ());
			me.setUserName (member.getUserName ());
			me.setVip (member.isVip ()); 
			
			memberDao.update (me);
		}
		
		
	}
	
	@Override
	public Member getMemberByMap(Map<String, Object> params)
	{
		Assert.notEmpty (params);
		
		Member member = memberDao.getByMap (params);
		
		if(ObjectUtils.notNull (member))
		{
			member.setPassword (MD5.convertMD5 (member.getPassword ()));
		}
		
		return member;
	}
	
	@Override
	public Member getMemberById(long id)
	{
		
		
		Member member = memberDao.getById (id);
		
		if(ObjectUtils.notNull (member))
		{
			member.setPassword (MD5.convertMD5 (member.getPassword ()));
		}
		
		return member;
	}

	@Override
	public void logOut(long memberId)
	{
		Member member = memberDao.getById (memberId);
		
		if(ObjectUtils.notNull (member))
		{
			member.setLoginStatus (2); //状态为2,表示为下线
			
			memberDao.update (member);
			kongjianService.logOut (memberId);
		}
	}
	
}
